package com.lan.springframework.beans.factory.support;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

import com.lan.springframework.beans.BeansException;
import com.lan.springframework.beans.factory.config.BeanDefinition;

/**
 * @author Keason
 * @version 创建时间：2022年11月15日 下午10:46:59
 * @TODO
 * 
 */
public class SimpleInstantiationStrategy implements InstantiationStrategy{
	 @Override
	    public Object instantiate(BeanDefinition beanDefinition, String beanName, Constructor ctor, Object[] args) throws BeansException {
	        Class clazz = beanDefinition.getBeanClass();
	        try {
	            if (null != ctor) {
	                return clazz.getDeclaredConstructor(ctor.getParameterTypes()).newInstance(args);
	            } else {
	                return clazz.getDeclaredConstructor().newInstance();
	            }
	        } catch (NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException e) {
	            throw new BeansException("Failed to instantiate [" + clazz.getName() + "]", e);
	        }
	    }
}
